package com.gugee.ins.common.dal.mapper.statistics;

import com.gugee.common.mapper.GugeeMapper;
import com.gugee.ins.common.model.mysql.statistics.InsStBloggerCfsAvg;
import com.gugee.ins.data.common.msg.InsMBIdenticalLevelMessage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface InsStBloggerCfsAvgMapper extends GugeeMapper<InsStBloggerCfsAvg> {

    @Select("select * from `ins_st_blogger_cfs_avg` where category = #{category} and follower_size = #{sizeId}")
    InsStBloggerCfsAvg findByCategoryAndFollowerSize(String category, long sizeId);

    @Select("select id from `ins_st_blogger_cfs_avg` order by id  limit #{start},#{row}")
    List<InsMBIdenticalLevelMessage> getInsMBIdenticalLevelMessageList(int start, int row);

    /**
     * 更新同量级同分类平均值
     * @param category
     * @param followerSize
     * @param now
     */
    @Update("UPDATE ins_st_blogger_cfs_avg t1," +
            "(select IFNULL(avg(brand_media_er),0)brand_media_er,IFNULL(avg(avg_er),0)avg_er," +
            " IFNULL(avg(last_follower_grow_rate),0)follower_grow_rate,IFNULL(avg(real_follower_rate),0)real_follower_rate " +
            " from ins_blogger_main where follower_size = #{followerSize} and overall_category_name = #{category} and account_status = 0  )t2 " +
            " set t1.ad_media_er = t2.brand_media_er,t1.media_er = t2.avg_er,t1.follower_growth_rate = t2.follower_grow_rate, " +
            " t1.real_follower_rate = t2.real_follower_rate ,t1.last_st_time = #{now} " +
            " where t1.follower_size = #{followerSize} and t1.category = #{category} ")
    void updateStBloggerCfsAvg(@Param("category")String category, @Param("followerSize")int followerSize,
                               @Param("now")long now);
}
